package com.jiunuan.live.framework.redis.starter.key;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Conditional;

@Configurable
@Conditional(RedisKeyLoadMatch.class)
public class IMCoreServerProviderCacheKeyBuilder extends RedisKeyBuilder{

    private static final String IM_ONLINE_ZSET = "imOnlineZSet";
    public static final String IM_BIND_IP_KEY = "jiunuan-live-im-bind-ip:";
    public static final String IM_ACK_MAP = "imAckMap";


    /**
     * 构建用户在线状态的有序集合键名
     * 该方法用于生成特定用户在线状态的有序集合键名，键名由前缀、特定的字符串、分隔符和经过取模运算处理的用户ID组成
     * 取模运算是为了实现键名的哈希分布，提高数据存取的效率
     *
     * @param userId 用户ID，用于生成最终的键名
     * @return 组合后的键名字符串
     */
    public String buildIMOnlineZSet(Long userId,Integer appId) {
        // 需要对userId取模
        return super.getPrefix() + IM_ONLINE_ZSET + super.getSplitItem() + appId
                + super.getSplitItem() + userId % 10000;
    }

    public String buildIMBindIpKey(Integer appId,Long userId) {
        return IM_BIND_IP_KEY + appId + super.getSplitItem() + userId;
    }

    /**
     * 构建ack消息的map键名,消息重试量不大，所以取模100即可
     * @param userId
     * @param appId
     * @return
     */
    public String buildIMAckMapKey(Long userId,Integer appId) {
        return super.getPrefix() + IM_ACK_MAP + super.getSplitItem() + appId + super.getSplitItem() + userId % 100;
    }
}
